<?php
$in_site = 'dassnfu';
$page = 'Scheidsrechters';
$loginVereist = 1;
$overigeVariabelen['paginaNietAf'] = true;
include_once($_SERVER['DOCUMENT_ROOT']."/start.php");

//Ben je wel ingelogd?
if(empty($_SESSION['spelerId'])) {
	$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />";
	$no_style = true;
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	echo "<div class='error'><p class='icon'>Login om deze pagina te bekijken.</p></div>";
	include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
	die();
}
//Heb je wel het recht om deze pagina te bekijken?
if(($_SESSION['rechten'] & (1 << $rechten['scheidsrechters_beheren'])) == 0) {
	$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />";
	$no_style = true;
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	echo "<div class='error'><p class='icon'>Je hebt niet het recht om iets aan het scheids schema toe te voegen.</p></div>";
	include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
	die();
}

include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/database_connectie.php");

$error = "";
$bestanden = "";
if(isset($_POST['toevoegen'])) {
	//Zijn alle waardes ingevuld?
	if(empty($_POST['teamId']) || empty($_POST['datum']) ||empty($_POST['starttijd'])) {
		$error .= "<div class='error'><p class='icon'>Niet alle verplichte velden zijn ingevuld (team, datum, starttijd)</p></div>";
	}else {
		//Mag je de activiteit wel toevoegen aan het geselecteerde team?
		//TODO
		
		//Is de datum en starttijd wel een goede geldige datum
		$starttijd = date('Y-m-d H:i', strtotime($_POST['datum']." ".$_POST['starttijd']));
		if($starttijd == '1970-01-01 01:00'){
			$error .= "<div class='error'><p class='icon'>Er is geen geldige datum of starttijd ingevuld.</p></div>";
		}
	}
	
	if($error == "") {
		//Voeg de scheidsrechters toe
		$sql_gelukt = mysql_query("INSERT INTO Scheidsrechter (teamId, starttijd, wedstrijdcode, thuisploeg, tegenstander, scheids1, scheids2, afgelast)
			VALUES('".$_POST['teamId']."', '".$starttijd."', '".$_POST['wedstrijdcode']."', '".$_POST['thuisploeg']."', '".$_POST['tegenstander']."',
			".(empty($_POST['persoon1']) ? "NULL" : "'".$_POST['persoon1']."'").",
			".(empty($_POST['persoon2']) ? "NULL" : "'".$_POST['persoon2']."'").",
			'".(isset($_POST['aflasting']) ? "1" : "0")."')");
		
		if($sql_gelukt) {
			//Zorg ervoor dat de post wordt gedaan, want het toevoegen is gelukt
			$bestanden = "<script type='text/javascript' src='/bestanden/js/post/js_post.js'></script>";
			$bestanden .= "<script type='text/javascript'>window.onload = function() { post_to_url(\"./\", {i:\"<div class='succes'><p class='icon'>De scheidsrechters zijn succesvol toegevoegd.</p></div>\"}); };</script>";
		}else if($bestanden == "") {
			//Er is iets mis gegaan tijdens het verzenden van het bericht
			$error .= "<div class='error'><p class='icon'>Tijdens het versturen van de gegevens is er iets mis gegaan.</p></div>";
		}
	}
}


if(isset($_POST['toevoegen']) && $error == "") {
	include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/quick_post.php");
	die();
}else {
	$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/css/style_tabellen".$style_nmr.".css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />
	<!-- BBcodes -->
	<link rel='stylesheet' type='text/css' href='/bestanden/js/bbcode/css/write_bbcode".$style_nmr.".css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/bbcode/css/read_bbcode".$style_nmr.".css' />
	<script type='text/javascript' src='/bestanden/js/bbcode/js/bbcode.js'></script>
	<!-- JSCalender -->
	<script type='text/javascript' src='/bestanden/js/kalender/js/jscal2.js'></script>
	<script type='text/javascript' src='/bestanden/js/kalender/lang/en.js'></script>
	<link rel='stylesheet' type='text/css' href='/bestanden/js/kalender/css/jscal2.css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/kalender/css/border-radius.css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/kalender/css/steel/steel.css' />
	<!-- Timepicker -->
	<link rel='stylesheet' type='text/css' href='/bestanden/js/timepicker/css/timepicker".$style_nmr.".css' />
	<script type='text/javascript' src='/bestanden/js/timepicker/js/jquery.timepicker.js'></script>
	<!-- set JSCalendar en de timepicker -->
		<script type='text/javascript'>
		$(document).ready(function() {
			new Calendar({
				inputField: 'datum_trigger',
				dateFormat: '%d %B %Y',
				trigger: 'datum_trigger',
				showTime: false,
				bottomBar: false,
				onSelect: function() {this.hide();}
			});
			
			$('.tijd').timePicker({
				startTime: '09:00',  // Using string. Can take string or Date object.
				endTime: '21:00',  // Using Date object.
				show24Hours: true,
				separator:':',
				step: 15
			});
			
			//Informatie weergeven of niet
			//$('.informatie_opmerking_trigger').click(function() {
				//$('.informatie_opmerking').fadeIn(500);
				//$('.scheidsrechters_toevoegen').hide();
				//$('.informatie_opmerking_trigger').hide();
			//});
		});
		</script>";
	
	$no_style = true;
	include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/instellingen.php");
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	echo $error;
	
	$alles_goed = true;
	//Seizoen
	$jaar = date('y');
	//$jaar = 2011;
	
	//Team
	echo "<form action='./toevoegen.php".(isset($_GET['team']) ? "?team=".$_GET['team'] : "")."' method='post'>";
	echo "<table class='selecties'>";
	$result = mysql_query("SELECT DISTINCT Team.teamId, Team.team
		FROM Teambeheer, Team
		WHERE Teambeheer.spelerId = '".$spelerId."' AND Teambeheer.rechten & (b'1' << ".$rechten['scheidsrechters_beheren'].") AND Teambeheer.van <= NOW() AND (Teambeheer.tot >= NOW() OR Teambeheer.tot IS NULL)
			AND Team.teamId = Teambeheer.teamId
		ORDER BY Team.team");
	$team = team_selecteren($result, "./toevoegen.php?", true, (isset($_GET['team']) ? $_GET['team'] : 0));
	if($team == -1) {	echo "<div class='error'><p class='icon'>Je hebt niet het recht om een kantine item toe te voegen bij een team.</p></div>";		$alles_goed = false;}
	echo "</table>";
	
	if($alles_goed == true) {
		echo "<input type='hidden' name='teamId' value='".$team."'>";
		echo "<table class='met_border submit'>";
			echo "<tr class='normaal'><th colspan='2'>Scheidrechteren</th></tr>";
			//Datum
			echo "<tr class='normaal'>";
				echo "<td class='left_align'>Datum</td>";
				echo "<td class='left_align'><span class='textfield'><input type='text' id='datum_trigger' name='datum' value='".(empty($_POST['datum']) ? "Klik hier" : $_POST['datum'])."' READONLY style='width: 120px;'></span></td>";
			echo "</tr>";
			//Tijd
			echo "<tr class='normaal'>";
				echo "<td class='left_align'>Starttijd</td>";
				echo "<td class='left_align'><span class='textfield'><input type='text' class='tijd' name='starttijd' value='".(isset($_POST['starttijd']) ? $_POST['starttijd'] : "")."' style='width: 35px;'></span></td>";
			echo "</tr>";
			//Wedstrijdcode
			echo "<tr class='normaal'>";
				echo "<td class='left_align'>Wedstrijdcode</td>";
				echo "<td class='left_align'><span class='textfield'><input type='text' name='wedstrijdcode' value='".(isset($_POST['wedstrijdcode']) ? $_POST['wedstrijdcode'] : "")."' style='width: 120px;'></span></td>";
			echo "</tr>";
			//Thuisploeg
			echo "<tr class='normaal'>";
				echo "<td class='left_align'>Thuisploeg</td>";
				echo "<td class='left_align'><span class='textfield'><input type='text' name='thuisploeg' value='".(isset($_POST['thuisploeg']) ? $_POST['thuisploeg'] : "")."' style='width: 120px;'></span></td>";
			echo "</tr>";
			//Tegenstander
			echo "<tr class='normaal'>";
				echo "<td class='left_align'>Tegenstander</td>";
				echo "<td class='left_align'><span class='textfield'><input type='text' name='tegenstander' value='".(isset($_POST['tegenstander']) ? $_POST['tegenstander'] : "")."' style='width: 120px;'></span></td>";
			echo "</tr>";
			
			//Lege rij
			echo "<tr class='klein'><td colspan='".(isset($row->dubbelMet) ? "3" : "2")."'>&nbsp;</td></tr>";
			
			//Personen ophalen
			$result = mysql_query("SELECT DISTINCT Speler.spelerId, Speler.spelerId, Speler.voornaam, Speler.tussenvoegsel, Speler.achternaam FROM Speler, Teambeheer
				WHERE Teambeheer.teamId = '".$team."' AND Teambeheer.van <= NOW() AND (Teambeheer.tot >= NOW() OR Teambeheer.tot IS NULL)
					AND Speler.spelerId = Teambeheer.spelerId
				ORDER BY Speler.achternaam, Speler.voornaam, Speler.tussenvoegsel, Speler.spelerId");
			if($result == null || mysql_num_rows($result) == 0) {
				echo "<tr class='normaal'>";
					echo "<td class='left_align' colspan='2'><div class='error'><p class='icon'>Er zijn geen personen die bij het geselecteerde team spelen.</p></div></td>";
				echo "</tr>";
			}else {
				//Voeg 2 rijen toe om personen te selecteren
				echo "<tr class='normaal'>";
					echo "<td class='left_align' rowspan='2'>Personen</td>";
				for($i = 1; $i <= 2; $i++) {
					if($i > 1) {
						echo "<tr class='normaal'>";
					}	
						echo "<td class='left_align left_border'>";
							echo "<select name='persoon".$i."'>";
								mysql_data_seek($result, 0);
								echo "<option value='0'> - </option>";
								while($row = mysql_fetch_array($result)) {
									echo "<option value='".$row['spelerId']."'".(isset($_POST['persoon'.$i]) && $_POST['persoon'.$i] == $row['spelerId'] ? " selected='yes'" : "").">".$row['voornaam']." ".(isset($row['tussenvoegsel']) ? $row['tussenvoegsel']." " : "").$row['achternaam']."</option>";
								}
							echo "</select>";
						echo "</td>";
					echo "</tr>";
				}
			}
			
			//Lege rij
			echo "<tr class='klein'><td colspan='".(isset($row->dubbelMet) ? "3" : "2")."'>&nbsp;</td></tr>";
			
			//Afgelasting
			echo "<tr class='normaal'>";
				echo "<td class='left_align'>Afgelasting</td>";
				echo "<td class='left_align'><input type='checkbox' name='aflasting' value='yes' ".((isset($_POST['aflasting']) && $_POST['aflasting'] == 'yes') ? "checked='yes'" : "")."></td>";
			echo "</tr>";
			
			if(isset($row->dubbelMet)) {
				//Om de eventuele info juist weer te geven
				$bericht .= $row2->bericht;
			}
		echo "</table>";
		
		/*
		 * De tekstarea voor de informatie
		 */
		
		//if(empty($_POST['bericht'])) {
			//echo "<a class='button2 informatie_opmerking_trigger'><span>Informatie toevoegen</span></a>";
			echo "<span class='button scheidsrechters_toevoegen'><input type='submit' name='toevoegen' value='Scheidsrechters toevoegen'></span>";
		//}
		/*
		
		echo "<div class='informatie_opmerking' ".(empty($_POST['bericht']) ? "style='display: none;'" : "").">";
			echo "<div class='button_bar'>";
				echo "<div class='bbcode_buttons'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/bold.gif' onclick='insert_bbcode(\"[b]\", \"[/b]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/italic.gif' onclick='insert_bbcode(\"[i]\", \"[/i]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/underline.gif' onclick='insert_bbcode(\"[u]\", \"[/u]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/ordered.gif' onclick='insert_bbcode(\"[list=1][*]\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/unordered.gif' onclick='insert_bbcode(\"[list][*]\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/link.gif' onclick='insert_prompt_bbcode(\"[url]\", \"[/url]\", \"bericht\", \"Geef de URL op\", \"http://\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/image.gif' onclick='insert_prompt_bbcode(\"[img]\", \"[/img]\", \"bericht\", \"Geef de locatie van het plaatje op\", \"http://\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/quote.gif' onclick='insert_bbcode(\"[quote]\", \"[/quote]\", \"bericht\");'>";
					echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/code.gif' onclick='insert_bbcode(\"[code]\", \"[/code]\", \"bericht\");'>";
				echo "</div>";
				echo "<div class='bbcode_emoticons'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/bigsmile.gif' onclick='insert_bbcode(\":D\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/smile.gif' onclick='insert_bbcode(\":)\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/laugh.gif' onclick='insert_bbcode(\"XD\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/tongue.gif' onclick='insert_bbcode(\":P\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/wink.gif' onclick='insert_bbcode(\":D\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/frown.gif' onclick='insert_bbcode(\":(\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/surprise.gif' onclick='insert_bbcode(\":O\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/worry.gif' onclick='insert_bbcode(\":s\", \"\", \"bericht\");'>";
					echo "<img class='bbcode_emoticon' src='/images/style/emoticons/sweatdrop.gif' onclick='insert_bbcode(\"^^\", \"\", \"bericht\");'>";    
				echo "</div>";
			echo "</div>";
			echo "<div class='clr'></div>";
			//Het teklstvak
			echo "<div class='textarea'>";
				echo "<textarea id='bericht' class='klein' name='bericht' maxlength='1000'>".(isset($_POST['bericht']) ? $_POST['bericht'] : "")."</textarea>";
			echo "</div>";
			//Symbool keuze
			echo "<div class='symbool_keuze'>";
				echo "<p>Het te gebruiken symbool: ";
				echo "<label for='symbool_let_op_0'><img src='/images/style".$style_nmr."/icons/informatie.png'></label> <input id='symbool_let_op_0' type='radio' name='symbool_let_op' value='0'".((empty($_POST['symbool_let_op']) || $_POST['symbool_let_op'] == '0') ? " checked" : "").">";
				echo "<label for='symbool_let_op_1'><img src='/images/style".$style_nmr."/icons/let_op.png'></label> <input id='symbool_let_op_1' type='radio' name='symbool_let_op' value='1'".((isset($_POST['symbool_let_op']) && $_POST['symbool_let_op'] == '1') ? " checked" : "")."></p>";
			echo "</div>";
			//De submit knoppen
			echo "<span class='button2'><input type='submit' name='voorbeeld' value='Voorbeeld'></span> ";
			echo "<span class='button'><input type='submit' name='toevoegen' value='Kantine dienst toevoegen'></span>";
			
			if(isset($_POST['voorbeeld'])) {
				include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/nbbc_parser/initialize_nbbc.php");
				
				echo "<div class='voorbeeld'>";
					echo "<p>Voorbeeld<p>";
					echo "<div class='entry'>";
						if(isset($_POST['bericht'])) {	$bbcode->Parse($_POST['bericht']);}
					echo "</div>";
				echo "</div>";
			}
			*/
	}
	echo "</form>";
}
include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");